﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark
{
    public partial class GioHang : System.Web.UI.Page
    {
        protected void Page_PreRender(object sender, EventArgs e)
        {
            Dictionary<long, SAN_PHAM> Cart = Session["Cart"] as Dictionary<long, SAN_PHAM>;
            content.InnerHtml = GenerateCartHtml(Cart);
            count.InnerHtml = Cart.Count.ToString();
            
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if (Request.QueryString["UpdateCart"] != null && Request.QueryString["ID"] != null && Request.QueryString["SL"]!=null)
                {
                    Dictionary<long, SAN_PHAM> Cart = Session["Cart"] as Dictionary<long, SAN_PHAM>;
                    long ID = long.Parse(Request.QueryString["ID"].ToString());
                    int SL = int.Parse(Request.QueryString["SL"].ToString());
                    if (Cart.ContainsKey(ID))
                    {
                        SAN_PHAM h = Cart[ID] as SAN_PHAM;
                        h.SoLuong = SL;
                        long? tt = 0;
                        string tien = string.Empty;

                        Response.Clear();
                        foreach (KeyValuePair<long, SAN_PHAM> item in Cart)
                        {
                            if (item.Value.NgayBatDau <= DateTime.Now && item.Value.NgayKetThuc >= DateTime.Now)
                            {
                                long? thanhtien = (item.Value.DonGia - ((item.Value.DonGia * item.Value.GiamGia) / 100)) * item.Value.SoLuong;
                                tt += thanhtien;
                            }
                            else
                            {
                                tt += item.Value.DonGia * item.Value.SoLuong;
                            }
                        }
                        if (h.NgayBatDau <= DateTime.Now && h.NgayKetThuc >= DateTime.Now)
                        {
                            long? thanhtien = (h.DonGia - ((h.DonGia * h.GiamGia) / 100)) * h.SoLuong;
                            tien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                    "{0:0,0}", thanhtien);
                        }
                        else
                        {
                            tien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                    "{0:0,0}", h.DonGia * h.SoLuong);
                        }
                        string tongtien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", tt) + " đ";
                        string vat = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                     "{0:0,0}", (tt * 10) / 100) + " đ";
                        string ttt = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                     "{0:0,0}", tt + (tt * 10 / 100)) + " đ";
                        Response.Write(tien + "@@@" + tongtien + "@@@" + vat + "@@@" + ttt);
                        Response.End();
                    }
                }
                if(Request["DeleteCart"]!=null && Request["ID"]!=null)
                {
                    long ID = long.Parse(Request.QueryString["ID"].ToString());
                    Dictionary<long, SAN_PHAM> Cart = Session["Cart"] as Dictionary<long, SAN_PHAM>;
                    Cart.Remove(ID);
                    long? tt = 0;
                    foreach (KeyValuePair<long, SAN_PHAM> item in Cart)
                    {
                        if (item.Value.NgayBatDau <= DateTime.Now && item.Value.NgayKetThuc >= DateTime.Now)
                        {
                            long? thanhtien = (item.Value.DonGia - ((item.Value.DonGia * item.Value.GiamGia) / 100)) * item.Value.SoLuong;
                            tt += thanhtien;
                        }
                        else
                        {
                            tt += item.Value.DonGia * item.Value.SoLuong;
                        }
                    }
                    string tongtien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", tt) + " đ";
                    string vat = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                 "{0:0,0}", (tt * 10) / 100) + " đ";
                    string ttt = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                                 "{0:0,0}", tt + (tt * 10 / 100)) + " đ";
                    Response.Clear();
                    Response.Write(tongtien + "@@@" + vat + "@@@" + ttt);
                    Response.End();
                }
            }
            
        }

        private string GenerateCartHtml(Dictionary<long,SAN_PHAM> Cart)
        {
            StringBuilder str = new StringBuilder();
            EMartEntities db = new EMartEntities();
            long? tt = 0;
            foreach (KeyValuePair<long, SAN_PHAM> item in Cart)
            {
                str.Append("<div id='row"+item.Key+"' class='container'>");
                str.Append("<div class='row'>");
                str.Append("<div class='product'>");
                str.Append("<div class='col-md-2 col-sm-3 hidden-xs p-wr'>");
                str.Append("<div class='product-attrb-wr'>");
                str.Append("<div class='product-attrb'>");
                str.Append("<div class='image'> <a class='img' href='ChiTietSP.aspx?ID=" + item.Key + "'><img alt='product info' src='images/products/" + db.HINH_SAN_PHAM.Where(s=>s.IDSanPham == item.Key).First().Hinh+ "' title='product title'></a> </div>");
                str.Append("</div>");
                str.Append("</div>");
                str.Append("</div>");
                str.Append("<div class='col-md-5 col-sm-7 col-xs-9 p-wr'>");
                str.Append("<div class='product-attrb-wr'>");
                str.Append("<div class='product-meta'>");
                str.Append("<div class='name'><h3><a href='ChiTietSP.aspx?ID="+item.Key+"'>"+item.Value.TenSanPham+"</a></h3></div>");
                if(item.Value.NgayBatDau<=DateTime.Now&& item.Value.NgayKetThuc>=DateTime.Now)
                {
                    long? thanhtien = item.Value.DonGia-((item.Value.DonGia*item.Value.GiamGia)/100);
                    
                    string tien =  String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", thanhtien);
                    str.Append("<div class='price'><span class='price-new'>" + tien + "<span class='sym'>đ</span></span>");
                    str.Append(" <span class='price-old'>"+item.Value.DonGia+"<span class='sym'>đ</span></span> </div>");
                }
                else
                {
                    string tien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", item.Value.DonGia);
                    
                    str.Append("<div class='price'><span class='price-new'>" + tien + "<span class='sym'>đ</span></span></div>");
                }
                    
                str.Append("</div></div></div>");
                str.Append("<div class='col-md-2 hidden-sm hidden-xs p-wr'>");
                str.Append("<div class='product-attrb-wr'>");
                str.Append("<div class='product-attrb'>");
                str.Append("<div class='qtyinput'>");
                str.Append("<div class='quantity-inp'>");
                str.Append("<input type='text' id='" + item.Key + "' data-id='" + item.Key + "' class='quantity-input' name='p_quantity' value='" + item.Value.SoLuong + "'>");
                str.Append("<div data-id='" + item.Key + "' class='quantity-txt minusbtn'><a href='#a' class='qty qtyminus' ><i class='fa fa-minus fa-fw'></i></a></div>");
                str.Append("<div data-id='" + item.Key + "' class='quantity-txt plusbtn'><a href='#a' class='qty qtyplus' ><i class='fa fa-plus fa-fw'></i></a></div>");
                str.Append("</div></div></div></div></div>");
                str.Append("<div class='col-md-2 hidden-sm hidden-xs p-wr'>");
                str.Append("<div class='product-attrb-wr'>");
                str.Append("<div class='product-attrb'>");
                if(item.Value.NgayBatDau<=DateTime.Now&& item.Value.NgayKetThuc>=DateTime.Now)
                {
                    long? thanhtien = (item.Value.DonGia - ((item.Value.DonGia * item.Value.GiamGia) / 100))*item.Value.SoLuong;
                    tt += thanhtien;
                    string tien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", thanhtien);
                    str.Append("<div class='price'> <span id='p"+item.Key+"' class='t-price'>" + tien + "</span><span class='sym t-price'>đ</span></div>");
                }
                else
                {
                    string tien = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", item.Value.DonGia * item.Value.SoLuong);
                    tt += item.Value.DonGia * item.Value.SoLuong;
                    str.Append("<div class='price'> <span id='p" + item.Key + "' class='t-price'>" + tien + "</span><span class='sym t-price'>đ</span> </div>");
                }
                
                str.Append("</div></div></div>");
                str.Append("<div class='col-md-1 col-sm-2 col-xs-3 p-wr'>");
                str.Append("<div class='product-attrb-wr'>");
                str.Append("<div class='product-attrb'>");
                str.Append("<div data-id='"+item.Key+"' class='remove'> <a href='#a' class='color2' data-toggle='tooltip' data-original-title='Remove'><i class='fa fa-trash-o fa-fw color2'></i></a> </div>");
                str.Append("</div></div></div></div></div></div>");
                str.Append("<div class='row clearfix f-space10'></div>");
            }
            TongT.InnerHtml = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", tt)+" đ";
            vat.InnerHtml = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", (tt * 10) / 100) + " đ";
            tongthanhtien.InnerHtml = String.Format(System.Globalization.CultureInfo.InvariantCulture,
                                         "{0:0,0}", tt +( tt * 10 / 100)) + " đ";
            return str.ToString();
        }
    }
}